home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / ccomment.zip / CCOMMENT.C next >
C/C++ Source or Header  |  1994-03-07  |  3KB  |  113 lines

  1.  
  2. /* ccomment.c */
  3. /* "C" and "C++" comment modifier */
  4.  
  5. /* Copyright 1994 (c) by Andrew Brault */
  6. /* You may use this code for any purpose you want. */
  7. /* I hold no responsibility for anything you may screw up with this code. */
  8. /* (nyeah, nyeah, nyeah!) */
  9.  
  10. /* Syntax: */
  11. /*  CCOMMENT <in-file> <out-file> [options, ...] */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. static short find_switch(int arg_c, char ** arg_v, char * s);
  17.  
  18. void main(int argc, char ** argv)
  19. {
  20.   char line[1007];
  21.   short continued = 0;
  22.   short mode, count, spot;
  23.   FILE * infile, * outfile;
  24.  
  25.   if(argc < 3) {
  26.     printf("CComment v1.0 (c) 1994  by Andrew Brault");
  27.     printf("\nSyntax: %s <infile> <outfile> [options, ...]", argv[0]);
  28.     printf("\nOptions:");
  29.     printf("\n /c       Convert C++ comments into C style /* */");
  30.     printf("\n /strip   Strip all comments from source file");
  31.     return;
  32.   }
  33.   mode = 0;
  34.   if(find_switch(argc, argv, "c")) mode = 1;
  35.   if(find_switch(argc, argv, "strip")) mode = 3;
  36.  
  37.   if(mode == 0) {
  38.     printf("\nToo few parameters -- specify either /c or /strip");
  39.     return;
  40.   }
  41.   infile = fopen(argv[1], "rt");
  42.   if(infile == NULL) {
  43.     printf("\nCould not open input file %s.", argv[1]);
  44.     return;
  45.   }
  46.   fseek(infile, 0, SEEK_SET);
  47.   outfile = fopen(argv[2], "wt");
  48.   if(outfile == NULL) {
  49.     printf("\nCould not open output file %s.", argv[2]);
  50.     return;
  51.   }
  52.   fseek(outfile, 0, SEEK_SET);
  53.  
  54.   while(feof(infile) == 0) {
  55.     fgets(line, 1000, infile);
  56.     for(count = 0; count != 1000; count++) {
  57.       if(line[count] == '\n' || line[count] == NULL) break;
  58.       if((continued && count == 0) || line[count] == '/') {
  59.         if(mode == 1) {
  60.           if(line[count+1] != '/') continue;
  61.           line[count+1] = '*';
  62.           /* Search for newline .. */
  63.           count += 2;
  64.           while(line[count] && line[count] != '\n') ++count;
  65.           line[count] = ' ';
  66.           line[count+1] = '*';
  67.           line[count+2] = '/';
  68.           line[count+3] = '\n';
  69.           line[count+4] = '\0';
  70.           break;
  71.         }
  72.         else {
  73.           /* Look for /* or // */
  74.           if(line[count+1] == '/') {
  75.             /* Erase the rest of the line (c++ style comment) */
  76.             line[count] = '\n';
  77.             line[count+1] = '\0';
  78.             break;
  79.           }
  80.           if(line[count+1] == '*' || continued) {
  81.             /* C style comment; these can span multiple lines */
  82.             /* Look for the end */
  83.             spot = count; /* Mark this spot */
  84.             while(line[count] && line[count] != '\n' &&
  85.                 !(line[count] == '*' && line[count+1] == '/'))
  86.               ++count;
  87.             /* Truncate this line */
  88.             line[spot] = '\n';
  89.             line[spot+1] = '\0';
  90.             if(line[count] == '\n' || line[count] == '\0')
  91.               continued = 1;
  92.             else continued = 0;
  93.             break;
  94.           }
  95.         }
  96.       }
  97.     }
  98.     if(!feof(infile)) fputs(line, outfile);
  99.   }
  100.   fclose(infile);
  101.   fclose(outfile);
  102. }
  103.  
  104. static short find_switch(int arg_c, char ** arg_v, char * s)
  105. {
  106.   short count;
  107.   for(count = 1; count < arg_c; count++) {
  108.     if(arg_v[count][0] == '-' || arg_v[count][0] == '/')
  109.       if(strcmp(arg_v[count]+1, s) == 0) return 1;
  110.   }
  111.   return 0;
  112. }
  113.